﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;

namespace BSCMFF.Web.Pages.Reporting
{
    public partial class EmployeesReport : BSCMFF.Core.MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillGridAveragePKPIsScore(28);
            FillGridPKPIsScore(28);
            FillGridCompetencies(28);
        }

        private void FillGridCompetencies(int p)
        {
            BSCEntities context = new BSCEntities();
            var data = (from qv in context.Evaluation_User_Question_Value
                        join eq in context.Evaluation_Question on qv.Evaluation_Question_FK equals eq.ID
                        join ec in context.Evaluation_Category on eq.Evaluation_Category_FK equals ec.ID
                        join ee in context.Evaluation_Evaluator_Employee on qv.EEE_FK equals ee.ID
                        join ep in context.Evaluation_Period on ee.Evaluation_FK equals ep.ID
                        //group ee by ee.Employee_FK into groupedEmployees
                        group qv by new { ee.Employee_FK, ec.ID } into g

                        select new
                        {
                            employeeID = g.Key.Employee_FK,
                            category = g.Key.ID,
                            perc = (g.Sum(qv => qv.Value) - g.Sum(ec => ec.Evaluation_Question.Evaluation_Category.Min)) * 100 / (g.Sum(ec => ec.Evaluation_Question.Evaluation_Category.Max) - g.Sum(ec => ec.Evaluation_Question.Evaluation_Category.Min))
                            //  //(SUM(qv.Value)-SUM(ec.min)) * 100 / ( SUM(ec.Max) - SUM(ec.min))
                        }).ToList();

            RadGrid rg = new RadGrid();
            rg.ID = "newrg";
            rg.DataSource = data;
            rg.DataBind();

            Page.Controls.Add(rg);
        }

        private void FillGridPKPIsScore(int evalID)
        {
            BSCEntities context = new BSCEntities();

            var data = (from epk in context.Evaluation_PKPIs
                        join ee in context.Evaluation_Evaluator_Employee on epk.EEE_FK equals ee.ID
                        where ee.Evaluation_FK == evalID
                        join u in context.Users on ee.Employee_FK equals u.ID
                        select new
                        {

                            empID = u.ID,
                            first = u.Firstname,
                            last = u.Lastname,
                            weight = epk.Weight,
                            score = epk.Score,
                            weightedScore = epk.Weight * epk.Score,
                            verbal = epk.VerbalEvaluation

                        });

            RadGrid2.DataSource = data;
            RadGrid2.DataBind();
        }

        private void FillGridAveragePKPIsScore(int evalID)
        {
            BSCEntities context = new BSCEntities();

            var data = (from epk in context.Evaluation_PKPIs
                        join ee in context.Evaluation_Evaluator_Employee on epk.EEE_FK equals ee.ID
                        where ee.Evaluation_FK == evalID
                        group epk by new { ee.Employee_FK } into g
                        join u in context.Users on g.Key.Employee_FK equals u.ID
                        orderby u.ID
                        select new
                        {
                            first = u.Firstname,
                            last = u.Lastname,
                            pkpi = g.Sum(epk => epk.Weight * epk.Score),
                            count = g.Select(x => x.ID).Count()

                        });

            RadGrid1.DataSource = data;
            RadGrid1.DataBind();
        }
    }
}